<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css"/>
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css"/>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script>
        function getCollectNums(strategyId){
            $.get("/strategycollect/"+strategyId,function (data) {
                console.log(data);
                $(".focus .collectnums").find("span").html(data);
        })
        }
        $(function () {
            var user = JSON.parse(sessionStorage.getItem("user"));
            var strategyId = getParams().strategyId;
            //发送请求获取当前的攻略信息
            $.get("/strategies/" + strategyId, function (data) {
                $("#title").html(data.title);
            });
            //发送请求获取当前的区域的游记信息
            $.get("/travels/travels",{strategyId:strategyId},function (data) {
                console.log(data);
                $("#pills-catalog .hot").renderValues({list:data},{
                    handleTravel:function (ele,value) {
                        console.log(value);
                        ele.href = "/travelContent.html?id="+value;
                    }
                });
            });

            //发送ajax请求获取当前攻略的分类
            $.get("/strategies/" + strategyId + "/catalogs", function (data) {
                var num = 1;
                $("#pills-catalog .catalogs").renderValues({list: data}, {
                    showTitle: function (ele, value) {
                        // 01/概况
                        var content = (num++) + "/" + value;
                        if (num < 10) {
                            content = 0 + content;
                        }
                        ele.innerHTML = content;
                    },
                    handle: function (ele, value) {
                        ele.href = "/strategyDetails.html?id=" + value;
                    }
                });
            });
            //点赞
            var user = JSON.parse(sessionStorage.getItem("user"));
            console.log(user);
            //如果没有登录
            if(user==null){
                $("#likeBtn").click(function (){
                    $(document).dialog({
                        type : "notice",
                        infoText: "亲,登录了才可以点赞哦",
                        autoClose: 1500,
                        position: "center"
                    });
                })
            }else{

                $.get("/likeb/"  +strategyId,function (data){
                   var num = data;
                    console.log(num);
                    $(".fa-thumbs-up").find("span").html(num)
                });


                likeState(user.id,strategyId);
                //点击按钮之前先加载按钮样式,看之前是否有点赞记录

                $("#likeBtn").click(function () {

                    if(sessionStorage.getItem("user")) {

                        // 点击按钮之后再次发送ajax请求,判断之前是否有过点赞记录,有点赞记录,删除点赞记录,没有点赞,保存点赞记录

                        $.post("/likeb/" + user.id + "/" + strategyId, function (data) {


                            if ($(".fa-thumbs-up").css("color") == "rgb(255, 0, 0)") {
                                $.get("/likeb/"  +strategyId,function (data){
                                    var num = data;
                                    console.log(num);
                                    $(".fa-thumbs-up").find("span").html(num)
                                });

                                $(".fa-thumbs-up").css("color","black");
                            }else {


                                $.get("/likeb/"  +strategyId,function (data){
                                    var num = data;
                                    console.log(num);
                                    $(".fa-thumbs-up").find("span").html(num)
                                });

                                $(".fa-thumbs-up").css("color","red");
                            }
                        });
                        // 再次调用函数likeState();来改变点击按钮后的样式
                        //likeState(user.id,id);


                    }
                });

                //查询是否有点过赞的记录
                function likeState(userId,strategyId) {
                    $.get("/likeb/" +user.id + "/" +strategyId,function (data){

                        if(data){
                            $(".fa-thumbs-up").css("color","red");
                        }else {
                            $(".fa-thumbs-up").css("color","");
                        }

                    })

                }
            }

            //点评按钮事件
            $("#commentBtn").click(function () {
                location.href = "/mine/addComment.html?strategyId=" + strategyId;
            })
            getCollectNums(strategyId);
            //点赞默认为false
            var flag = false;
            //判断是否登陆
            if (user) {
                //登陆了就判断是否收藏
                $.get("/strategycollect/getCollectByUserId/"+user.id, {strategyId: strategyId, state: 1}, function (data) {
                    if (data !=0 ) {
                        $("#collectBtn").css("color", "red");
                        flag = true;
                    } else {
                        $("#collectBtn").css("color", "black");
                        flag = false;
                    }
                })
            }
            //点击收藏
            $("#collectBtn").click(function () {
                //判断是否登陆 否就弹框
                if (user) {
                    //收藏
                    $.post("/strategycollect/" + user.id, {strategyId: strategyId}, function (data) {
                        if(data){
                            $("#collectBtn").css("color", "red");
                            flag = true;
                        }else {
                            $("#collectBtn").css("color", "black");
                            flag = false;
                        }
                        getCollectNums(strategyId);
                    })
                }else{
                    $(document).dialog({
                        type: "confirm",
                        style: "IOS",
                        titleShow: false,
                        content: "请先登录",
                    });
                }
            })

        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title" id="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart collectnums" style="color: black" id = "collectBtn"><span> </span> 收藏</i>
        <i class="fa fa-thumbs-up "  id="likeBtn"><span>0</span> 点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div render-loop="list">
                <a render-key="list.id" render-fun="handleTravel">
                    <button class="btn" render-html="list.title"></button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="showTitle"></h6>
                    <div render-loop="list.details">
                        <a href="strategyDetails.html" render-key="list.details.id" render-fun="handle">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>

            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>
                <span>很特别</span>
                <span>爱上这座城</span>
                <span>住宿方便</span>
                <span>刚刚</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a render-key="list.user.id" render-fun="handle">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star star" render-key="list.level" render-fun="star">

                        </span>
                        <span class="comment-date" render-html="list.time"></span>
                        <div class="comment-content">
                            <a render-key="list.id" render-fun="comment">
                                <p render-html="list.commentText"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgUrls">
                            <li>
                                <img render-src="list.imgUrls.imgUrl">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a render-key="list.id" render-fun="comment">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                                <i class="fa fa-thumbs-up"> <span>0</span> </i>
                            </a>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>

<script>
    var currentPage = 1;
    var strategy;
    var pages;
    $(function () {
        var strategyId = getParams().strategyId;
        //发送贾克斯请求查询有谁评论了
        $.get("/comments/" + strategyId,{pageSize:5}, function (data) {
            pages = data.pages;
            strategy = data.list;
            //渲染评论的数据
            renderViews(data);
        });
        //设置窗口的滚到事件
        $(window).scroll(function () {
            if (isEnd()) {
                if (currentPage < pages) {
                    currentPage++;
                    var args = {currentPage: currentPage, pageSize: 5};
                    $.get("/travels", args, function (data) {
                        //数据渲染
                        $.merge(strategy, data.list);
                        renderViews({list: strategy});
                    });
                } else {
                    $(document).dialog({
                        type : "notice",
                        infoText: "已经没啦!o(*￣▽￣*)o",
                        autoClose: 1500,
                        position: "bottom"
                    });
                }
            }
        });
    })
    function renderViews(data) {
        $("#comment").renderValues(data, {
            handle: function (ele, value) {
                ele.href = "/userProfiles.html?id=" + value;
            },
            star:function (ele, value) {
                $(ele).attr("star",value);
            },
            comment:function (ele, value) {
                ele.href = "/strategyComment.html?id=" + value;
            }
        });
        //渲染小星星
        var stars = $(".star");
        $.each(stars,function (index, ele) {
            var star = $(ele);
            var num = star.attr("star");
            for (var i = 0;i < num ;i++) {
                $("<i class=\"fa fa-star\"></i>").appendTo(star);
            }
            for (var i = 0;i < 5 - num ;i++) {
                $("<i class=\"fa fa-star-o\"></i>").appendTo(star);
            }
        })
    }
</script>

<div id="push"></div>
</body>
</html>